<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace Model\CourseBundle\Model;

use Base\BaseModel;
use Doctrine\Bundle\DoctrineBundle\Registry;

class CourseLessonArrangeModel extends BaseModel{

    //获取所有课程目录
    public function getAllCourseLessonArrange($courseId){
        $qb = $this->getQueryBuilder();
        $qb->select("*")
            ->where("course = ?")
            ->setParameter(0,$courseId)
            ->from("CourseLessonArrange");
        return $qb->execute()->fetchAll();
    }

    //获取所有课程目录并构建分类树
    public function getAllCourseLessonArrangeTree($courseId)
    {
        $categories = $this->getAllCourseLessonArrange($courseId);
        $CourseLessonArrangeTree = $this->buildeCatTreeArray($categories);
        $data = array();
        foreach ($CourseLessonArrangeTree as $class) {
            $prefix = "";
            if ($class["level"] > 0) {
                $prefix .="├";
                for ($i=0;$i<$class["level"];$i++ ) {
                    $prefix .="─";
                }
            }
            $class["title"] = $class["name"];
            $class["name"] = $prefix.$class["name"];
            $data[] = $class;
        }
        return $data;
    }

    //获取第一层课程目录【章】
    public function getRootCourseLessonArrange($courseId)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")
            ->where("level = ?")
            ->setParameter(0,"1")
            ->andWhere("course = ?")
            ->setParameter(1,$courseId)
            ->from("CourseLessonArrange");
        return $qb->execute()->fetchAll();
    }

    public function getOneArray($id,$courseId)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")
            ->where("id = ?")
            ->setParameter(0,$id)
            ->andWhere("course = ?")
            ->setParameter(1,$courseId)
            ->from("CourseLessonArrange");
        return $qb->execute()->fetch();
    }

    public function commWebAdd($params)
    {
        $default = array(
            "course"  => 0,
            "name"  => "章节标题未设置",
            "level" => 0,
            "orders"=> 0,
            "parent"=> 0,
            "info"  => "章节简介未设置",
        );
        foreach ($default as $k => &$v) {
            if (isset($params[$k])) {
                $v = $params[$k];
            }
        }
        if ($default["parent"] != 0 && $default["course"] != 0 ) {
            $parent = $this->getOneArray($default["parent"],$default["course"]);
            if (!$parent) {
                return false;
            } else {
                $default["level"] = $parent["level"] + 1;
            }
        }
        $qb = $this->getQueryBuilder();
        if (isset($params["id"])) {
            //编辑
            $qb->update("CourseLessonArrange");
            $qb->where("id = :id")->setParameter("id",$params["id"]);
            foreach ($default as $key => $val) {
                $qb->set("$key",":$key")->setParameter(":$key",$val);
            }
        } else {
            //新增
            $qb->insert("CourseLessonArrange");
            $keys = array_keys($default);
            $values = array_values($default);
            $insertKeys = array();
            foreach ($keys as $k => $val) {
                $insertKeys[$val] = "?";
            }
            $qb->values($insertKeys)->setParameters($values);
        }


        $qb->execute();
        if ( $qb->getState() === 1) {
            return $default;
        } else {
            return $qb->getState();
        }
    }
//
//    public function commWebEdit($params)
//    {
//        $default = array(
//            "course"  => 0,
//            "name"  => "章节标题未设置",
//            "level" => 0,
//            "orders"=> 0,
//            "parent"=> 0,
//        );
//        foreach ($default as $k => &$v) {
//            if (isset($params[$k])) {
//                $v = $params[$k];
//            }
//        }
//        if ($default["parent"] != 0 && $default["course"] != 0 ) {
//            $parent = $this->getOneArray($default["parent"],$default["course"]);
//            if (!$parent) {
//                return false;
//            } else {
//                $default["level"] = $parent["level"] + 1;
//            }
//        }
//        $qb = $this->getQueryBuilder();
//        $qb->insert("CourseLessonArrange");
//        $keys = array_keys($default);
//        $values = array_values($default);
//        $insertKeys = array();
//        foreach ($keys as $k => $val) {
//            $insertKeys[$val] = "?";
//        }
//        $qb->values($insertKeys)->setParameters($values);
//        $qb->execute();
//        if ( $qb->getState() === 1) {
//            return $default;
//        } else {
//            return $qb->getState();
//        }
//    }
}